﻿Imports System.IO

Partial Class Interfaz_frmRrhhEmpleados
    Inherits System.Web.UI.Page
    Private CL As New Logica.ModuloRHH.ControladorLogica
    Dim urlImage As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If CL.visibilidadesFormsInternos("RecursosHumanos", Session("user").ToString) = False Then
                Response.Redirect("Index.aspx")
            End If
            If Not Page.IsPostBack Then
                cargarDrop()
                cargarProfesiones()
            End If
        Catch ex As Exception
            Mensaje(ex.Message)
        End Try
    End Sub

#Region "Funciones"
    Public Sub Mensaje(ByVal msg As String)
        Try
            Dim alertScript As String = "<script language=JavaScript runat=server>"
            alertScript &= "alert('" & msg & "');"
            alertScript &= "</script>"
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
        Catch ex As Exception
        End Try
       
    End Sub

    Private Sub cargarEmpleado()
        Try
            Dim nombre As String = TxTNombre.Text
            Dim apellido As String = TxTapellido.Text
            Dim rut As String = TxTRut.Text
            Dim direccion As String = TxTdireccion.Text
            Dim telefono As String = TxTcarry.Text + "-" + TxTFono.Text
            Dim profesion As String = TxTProf.Text
            Dim edad As String = TxTEdad.Text
            Dim fechaInicioEmpresa As String = Me.CalFechaEntrada.obtieneValorFecha
            Dim fechaFinEmpresa As String = Me.CalFechaSalida.obtieneValorFecha
            Dim sexo As String = DropSex.Text
            Dim fechaNacimiento As String = DropDia.Text + "-" + DropMes.Text + "-" + DropAño.Text
            Dim nacionalidad As String = DropNac.Text
            Dim paisnacimiento As String = TextPais.Text
            Dim regionNacimiento As String = TextRegion.Text
            Dim comunaNacimiento As String = TextComNac.Text
            Dim ciudadNacimiento As String = TextCiudad.Text
            Dim provinciaRecidencia As String = TextProv.Text
            Dim ciudadResidencia As String = TextCity.Text
            Dim celular As String = DropDigCel.Text + "-" + TxTCelular.Text
            Dim email As String = TxTPreMail.Text + "@" + TxTpostMail.Text
            Dim estadoCivil As String = DropEstadoCivil.Text
            Dim cargasFamiliares As String = DropCargas.Text
            Dim numeroCalzado As String = TxTcalzado.Text
            Dim talla As String = TxTtalla.Text

            Dim nuevoEmpleado As Empleado = New Empleado(nombre, apellido, rut, direccion, telefono, profesion, edad, fechaInicioEmpresa, _
                                            fechaFinEmpresa, sexo, fechaNacimiento, nacionalidad, paisnacimiento, regionNacimiento, _
                                            comunaNacimiento, ciudadNacimiento, provinciaRecidencia, ciudadResidencia, celular, _
                                            email, estadoCivil, cargasFamiliares, numeroCalzado, talla, urlImage)

            CL.InsertarEmpleado(nuevoEmpleado)

            For i As Integer = 0 To ListProfesion.Items.Count - 1
                CL.InsertarEmpleadoProfesion(rut, ListProfesion.Items(i).Value, i + 1)
            Next

        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Sub cargarDrop()
        Try
            Dim count As Integer

            Me.DropAño.Items.Clear()
            Me.DropCargas.Items.Clear()
            Me.DropDia.Items.Clear()
            Me.DropDigCel.Items.Clear()
            Me.DropEstadoCivil.Items.Clear()
            Me.DropMes.Items.Clear()
            Me.DropNac.Items.Clear()
            Me.DropSex.Items.Clear()

            For count = 1 To 31
                Me.DropDia.Items.Add(count)
            Next

            For count = 1 To 12
                Me.DropMes.Items.Add(count)
            Next

            For count = 1960 To 2050
                Me.DropAño.Items.Add(count)
            Next

            For count = 0 To 10
                Me.DropCargas.Items.Add(count)
            Next

            For count = 7 To 9
                Me.DropDigCel.Items.Add("0" + count)
            Next

            ' Estados Civil
            Me.DropEstadoCivil.Items.Add("Soltero")
            Me.DropEstadoCivil.Items.Add("Casado")
            Me.DropEstadoCivil.Items.Add("Viudo")

            'Nacionalidades
            Me.DropNac.Items.Add("África")
            Me.DropNac.Items.Add("----------")
            Me.DropNac.Items.Add("argelina")
            Me.DropNac.Items.Add("egipcia")
            Me.DropNac.Items.Add("marroquí")
            Me.DropNac.Items.Add("ecuatoguineana")
            Me.DropNac.Items.Add("saharaui")
            Me.DropNac.Items.Add("")
            Me.DropNac.Items.Add("América")
            Me.DropNac.Items.Add("----------")
            Me.DropNac.Items.Add("canadiense")
            Me.DropNac.Items.Add("estadounidense")
            Me.DropNac.Items.Add("mexicana")
            Me.DropNac.Items.Add("costarricense")
            Me.DropNac.Items.Add("salvadoreña")
            Me.DropNac.Items.Add("guatemalteca")
            Me.DropNac.Items.Add("hondureña")
            Me.DropNac.Items.Add("nicaragüense")
            Me.DropNac.Items.Add("panameña")
            Me.DropNac.Items.Add("cubana")
            Me.DropNac.Items.Add("dominicana")
            Me.DropNac.Items.Add("haitiana")
            Me.DropNac.Items.Add("argentina")
            Me.DropNac.Items.Add("boliviana")
            Me.DropNac.Items.Add("brasileña")
            Me.DropNac.Items.Add("chilena")
            Me.DropNac.Items.Add("colombiana")
            Me.DropNac.Items.Add("ecuatoriana")
            Me.DropNac.Items.Add("paraguaya")
            Me.DropNac.Items.Add("peruana")
            Me.DropNac.Items.Add("uruguaya")
            Me.DropNac.Items.Add("venezolana")
            Me.DropNac.Items.Add("")
            Me.DropNac.Items.Add("Asia")
            Me.DropNac.Items.Add("----------")
            Me.DropNac.Items.Add("china")
            Me.DropNac.Items.Add("surcoreana")
            Me.DropNac.Items.Add("filipina")
            Me.DropNac.Items.Add("hindú")
            Me.DropNac.Items.Add("israelí")
            Me.DropNac.Items.Add("japonesa")
            Me.DropNac.Items.Add("libanesa")
            Me.DropNac.Items.Add("rusa")
            Me.DropNac.Items.Add("siria")
            Me.DropNac.Items.Add("vietnamita")
            Me.DropNac.Items.Add("")
            Me.DropNac.Items.Add("Europa")
            Me.DropNac.Items.Add("----------")
            Me.DropNac.Items.Add("albanesa")
            Me.DropNac.Items.Add("española")
            Me.DropNac.Items.Add("francesa")
            Me.DropNac.Items.Add("griega")
            Me.DropNac.Items.Add("italiana")
            Me.DropNac.Items.Add("polaca")
            Me.DropNac.Items.Add("portuguesa")
            Me.DropNac.Items.Add("rumana")
            Me.DropNac.Items.Add("turca")
            Me.DropNac.Items.Add("alemana")
            Me.DropNac.Items.Add("holandesa")
            Me.DropNac.Items.Add("autríaca")
            Me.DropNac.Items.Add("belga")
            Me.DropNac.Items.Add("bulgara")
            Me.DropNac.Items.Add("chipriota")
            Me.DropNac.Items.Add("danesa")
            Me.DropNac.Items.Add("eslovaca")
            Me.DropNac.Items.Add("eslovena")
            Me.DropNac.Items.Add("estona")
            Me.DropNac.Items.Add("finlandesa")
            Me.DropNac.Items.Add("hungara")
            Me.DropNac.Items.Add("irlandesa")
            Me.DropNac.Items.Add("inglesa")
            Me.DropNac.Items.Add("escocesa")
            Me.DropNac.Items.Add("letona")
            Me.DropNac.Items.Add("lituana")
            Me.DropNac.Items.Add("luxemburguesa")
            Me.DropNac.Items.Add("maltesa")
            Me.DropNac.Items.Add("checa")
            Me.DropNac.Items.Add("sueca")
            Me.DropNac.Items.Add("armenia")
            Me.DropNac.Items.Add("bielorrusa")
            Me.DropNac.Items.Add("bosnia")
            Me.DropNac.Items.Add("moldava")
            Me.DropNac.Items.Add("monegasca")
            Me.DropNac.Items.Add("montenegrina")
            Me.DropNac.Items.Add("noruega")
            Me.DropNac.Items.Add("serbia")
            Me.DropNac.Items.Add("suiza")
            Me.DropNac.Items.Add("ucraniana")
            Me.DropNac.Items.Add("")
            Me.DropNac.Items.Add("Oceanía")
            Me.DropNac.Items.Add("---------")
            Me.DropNac.Items.Add("australiana")
            Me.DropNac.Items.Add("papuana")
            Me.DropSex.Items.Add("Masculino")
            Me.DropSex.Items.Add("Femenino")
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Sub cargarProfesiones()
        Try
            Dim arrayProfesion As ArrayList = CL.ObtenerProfesion

            For i As Integer = 0 To arrayProfesion.Count - 1
                Dim list As New ListItem

                list.Text = DirectCast(arrayProfesion.Item(i), Profesion).Descripcion
                list.Value = DirectCast(arrayProfesion.Item(i), Profesion).ID

                Me.DropProfesiones.Items.Add(list)
            Next


        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Sub FuncionUpload()
        Try
            Dim sExt As String = String.Empty
            Dim sName As String = String.Empty
            ImgSubida.ImageUrl = ""

            If uploadFile.HasFile Then
                sName = uploadFile.FileName
                sExt = Path.GetExtension(sName)

                If ValidaExtension(sExt) Then
                    uploadFile.SaveAs(MapPath("~/otros/TrabajadoresMSH/" & sName))
                    ImgSubida.ImageUrl = "~/otros/TrabajadoresMSH/" & sName
                    urlImage = "~/otros/TrabajadoresMSH/" & sName
                    lblMensaje.Text = "Archivo cargado correctamente."
                Else
                    lblMensaje.Text = "El archivo no es de tipo imagen."
                End If
            Else
                lblMensaje.Text = "Seleccione el archivo que desea subir."
            End If
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub

    Private Function ValidaExtension(ByVal sExtension As String) As Boolean
        Try
            Select Case sExtension
                Case ".jpg", ".jpeg", ".png", ".gif", ".bmp"
                    Return True
                Case Else
                    Return False
            End Select
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
       
    End Function

#End Region

#Region "Botones"

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        cargarEmpleado()
    End Sub

    Protected Sub BtnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnUpload.Click
        FuncionUpload()
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Try

            For i As Integer = 0 To ListProfesion.Items.Count - 1
                If DropProfesiones.SelectedValue = ListProfesion.Items(i).ToString Then
                    Exit Sub
                End If
            Next

            Dim list As New ListItem
            list.Text = DropProfesiones.SelectedItem.ToString
            list.Value = DropProfesiones.SelectedValue
            ListProfesion.Items.Add(list)

        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try

    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            ListProfesion.Items.Remove(ListProfesion.SelectedItem)
        Catch ex As Exception
            Me.Mensaje(ex.Message)
        End Try
    End Sub
#End Region
    
    
    
  
End Class
